/*
 * @(#)ResourceManager.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.util;

import org.eoti.util.log.LogUtil;

import javax.jnlp.BasicService;
import javax.jnlp.ServiceManager;
import javax.jnlp.UnavailableServiceException;
import java.io.IOException;
import java.net.URL;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Scanner;

public class ResourceManager
{
	protected static final String PIPE_DELIM = "\\|";
	protected ResourceBundle bundle;

	public ResourceManager(String bundleName)
	{
		this(bundleName, Locale.getDefault());
	}

	public ResourceManager(String bundleName, Locale locale)
	{
		this(ResourceBundle.getBundle(bundleName, locale));
	}

	public ResourceManager(ResourceBundle bundle)
	{
		this.bundle = bundle;
	}

	public ResourceBundle getBundle(){return bundle;}
	public Locale getLocale(){return bundle.getLocale();}

	public URL getCodebase()
			throws IOException
	{
		try{
			try{
		    	BasicService bs = (BasicService) ServiceManager.lookup("javax.jnlp.BasicService");
		    	return bs.getCodeBase();
		    }catch (UnavailableServiceException use){
		    	// not running in JNLP
		    }

		    String thisClassName = "/org/eoti/util/ResourceManager.class";
		    URL url = this.getClass().getResource(thisClassName);
			if(url != null)
			{
				String urlPath = url.toExternalForm();
				int index = urlPath.indexOf(thisClassName);
				if(index != -1)
					urlPath = urlPath.substring(0, index);

				return new URL(urlPath);
			}

			return null;
	    }catch (Exception e){
	    	throw new IOException("Unable to obtain codebase.", e);
	    }
	}

	public String getString(String key)
	{
		try{
			return bundle.getString(key);
		}catch(MissingResourceException mre){
			LogUtil.warning(String.format("ResourceManager.getString failed to find key '%s'", key), mre);
			return "";
		}
	}

	public String[] getStrings(String key)
	{
		String values = getString(key);
		if( (values == null) || (values.length() == 0))
			return new String[]{};

		return values.split(PIPE_DELIM);
	}

	public String[] getStrings(String key, int limit)
	{
		String values = getString(key);
		if( (values == null) || (values.length() == 0))
			return new String[]{};

		return values.split(PIPE_DELIM, limit);
	}

	public Boolean getBoolean(String key)
	{
		String value = getString(key);
		if( (value == null) || (value.length() == 0) )
			return Boolean.FALSE;

		return Boolean.parseBoolean(value);
	}
	public Scanner getScanner(String key)
	{
		return new Scanner(getString(key));
	}

	public Integer getInt(String key)
	{
		return Integer.parseInt(getString(key));
	}
}
